package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO, HttpSession session) {
        log.info("员工登录：{}", employeeLoginDTO);
        //调用service方法查询数据库
        Employee employee = employeeService.login(employeeLoginDTO);

        //将数据库获取信息进行加密Jwt
        String token = JwtUtil.getToken(employee.getId());

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }
    /*
    * 新增员工
    *
    * */
    @PostMapping
    @ApiOperation(value = "新增员工")
    public Result admin(@RequestBody EmployeeDTO employeeDTO){
        log.info("添加员工：{}", employeeDTO);
        return employeeService.admin(employeeDTO);
    }
    /*
    * 分页查询
    *
    * */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询员工：{}", employeePageQueryDTO);
        return employeeService.page(employeePageQueryDTO);
    }
    /*
     *启用禁用
     * @param status 状态 0表示禁用1 表示启用
     * @param id  员工id
     * @return
    * */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "用户启用状态")
    public Result updentByIdStatus(@PathVariable Integer status,Long id){
        log.info("用户启用状态：{}",status);
    return employeeService.updentByIdStatus(status,id);
    }
    /*
     *修改员工第一步先根据id查询
     * @param status
     * @param id  员工id
     * @return
     * */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询：{}",id);
        return employeeService.getById(id);
    }
    /*
     *修改员工
     * @param status
     * @param id  员工id
     * @return
     * */
    @PutMapping
    @ApiOperation(value = "修改员工")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("修改员工：{}",employeeDTO);
        return employeeService.update(employeeDTO);
    }
}
